#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
pthread_t main_thread;
void* Routine(void* arg)//回调
{
  sleep(1);
  pthread_detach(main_thread);
  char* str=(char*) arg;
  int count=0;
  while(count<5)
  {
    printf("%s:pid:%d,ppid:%d,tid:%lu\n",str,getpid(),getppid(),pthread_self());
    sleep(1);
  // int ret= pthread_cancel(main_thread);
  //  printf("%d",ret);                                                 
   // printf("已经取消\n");
    count++;
  }
  return NULL;
}

int main()
{
  pthread_t tid[6];
  main_thread=pthread_self();
  for(int i=0;i<6;i++)
  {
    char* buffer=(char*)malloc(64);
    sprintf(buffer,"thread %d",i);
    pthread_create(&tid[i],NULL,Routine,(void*)buffer);
    printf("%s tid is:%lu\n",buffer,tid[i]);//通过create的输出型参数拿到线程ID
  }
  while(1)
  {
    printf("i am a main pthread:pid:%d,ppid:%d,tid:%lu\n",getpid(),getppid(),pthread_self());
    sleep(1);
  }
  printf("wait finish\n");
  return 0;
}
